/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid;

import com.blandware.atleap.webapp.taglib.core.BaseHandlerTag;
import com.blandware.atleap.webapp.util.core.RequestUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.taglib.TagUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;


/**
 * <p>This tag renders to hyperlink to last page. Body of link will be body of this tag.
 * <br />
 * This tag is only valid when nested within <em>pager</em> tag.
 * </p>
 * <p><a href="LastPageTag.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.11 $ $Date: 2006/08/03 10:08:20 $
 * @see FirstPageTag
 * @see PrevPageTag
 * @see NextPageTag
 * @jsp.tag name="lastPage"
 * body-content="scriptless"
 * @see com.blandware.atleap.webapp.taglib.core.grid.PagerTag
 */
public class LastPageTag extends BaseHandlerTag {
	protected transient final Log log = LogFactory.getLog(LastPageTag.class);

    /**
     * Processes the tag
     *
     * @throws JspException
     * @throws IOException
     */
	public void doTag() throws JspException, IOException {

		PageContext pageContext = (PageContext) getJspContext();

		// This tag is only valid when nested within 'pager' tag, so check this
		PagerTag parentPagerTag = (PagerTag) findAncestorWithClass(this, PagerTag.class);

		if ( parentPagerTag == null ) {
			JspTagException e = new JspTagException("Parent tag is invalid! This tag is only valid when nested within 'pager' tag");
			throw e;
		}

		GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);

		if ( log.isDebugEnabled() ) {
			log.debug("Current page number: " + parentGridTag.getCurrentPageNumber());
			log.debug("Last page number: " + parentPagerTag.getLastPageNumber());
		}

		// If current page is already last, skip body, otherwise generate hyperlink

		if ( parentGridTag.getCurrentPageNumber().intValue() < parentPagerTag.getLastPageNumber().intValue() ) {

			String pageUrl = parentGridTag.getPageUrl();

			Map parameterMap = RequestUtil.prepareParameterMap(parentGridTag.getParameterMap());
			parameterMap.put("pageNumber", parentPagerTag.getLastPageNumber().toString());
			parameterMap.put("gridName", parentGridTag.getName());
			pageUrl += "?" + RequestUtil.createQueryStringFromMap(parameterMap, "&");

			StringWriter sw = new StringWriter();
			StringBuffer sb = sw.getBuffer();
			sb.append("<a href=\"").append(pageUrl).append("\"").append(prepareAttributes()).append(">");
			getJspBody().invoke(sw);
			sb.append("</a>");

			TagUtils.getInstance().write(pageContext, sw.toString());
		}
	}

}
